package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.mapper.ApCarMapper;
import com.zhentao.mapper.ApOrdersMapper;
import com.zhentao.mapper.ApProductMapper;
import com.zhentao.model.common.app.pojo.ApCar;
import com.zhentao.model.common.app.vo.ApCarVo;
import com.zhentao.model.common.buiness.dto.AddDto;
import com.zhentao.model.common.buiness.dto.CarDto;
import com.zhentao.model.common.buiness.pojo.Orders;
import com.zhentao.model.common.buiness.pojo.Product;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApCarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 86150
* @description 针对表【ap_car】的数据库操作Service实现
* @createDate 2024-09-19 18:49:44
*/
@Service
public class ApCarServiceImpl extends ServiceImpl<ApCarMapper, ApCar>
    implements ApCarService{
@Resource
private ApCarMapper apCarMapper;
@Resource
private ApOrdersMapper ordersMapper;
@Resource
private ApOrdersMapper apOrdersMapper;
@Resource
private ApProductMapper apProductMapper;
    @Override
    public ResponseResult carList(ApCarVo apCarVo) {
        List<ApCarVo> apCarVos = apCarMapper.carList(apCarVo);
        return ResponseResult.okResult(apCarVos);
    }

    @Override
    public ResponseResult addCar(ApCar apCar) {
      QueryWrapper<ApCar> queryWrapper=new QueryWrapper<>();
      queryWrapper.lambda().eq(ApCar::getUserId,apCar.getUserId());
      queryWrapper.lambda().eq(ApCar::getProductId,apCar.getProductId());
        ApCar apCar1 = apCarMapper.selectOne(queryWrapper);
        if(apCar1!=null){
            int qu=apCar1.getQuantity();
            int xin=apCar.getQuantity()+qu;
            apCar1.setQuantity(xin);
            apCar1.setUpdateTime(new Date());
            apCarMapper.updateById(apCar1);
        }else {
            apCar.setUpdateTime(new Date());
            apCar.setCreateTime(new Date());
            apCarMapper.insert(apCar);
        }
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult deleteCar(Integer id) {
        int i = apCarMapper.deleteById(id);
        if(i==1){
            return ResponseResult.okResult("删除成功");
        }
        return ResponseResult.errorResult(400,"删除成功");
    }

    @Override
    public ResponseResult carByOrder(AddDto addDto) {
        if (addDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int[] carArray = addDto.getCarArray();
        for (int id : carArray) {
            ApCar apCar = apCarMapper.selectById(id);
            Orders orders = new Orders();
            orders.setApId(apCar.getUserId());
            orders.setCreateTime(new Date());
            orders.setNum(apCar.getQuantity());
            orders.setProductId(apCar.getProductId());
            orders.setStatus(3);
            orders.setBuId(1);
            orders.setTotalPrice(584900.00);
            orders.setAddressId(addDto.getAddressId());
            int insert = ordersMapper.insert(orders);
            if (insert == 1) {
                QueryWrapper<ApCar> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(ApCar::getId, apCar.getId());
                ApCar car = apCarMapper.selectOne(queryWrapper);
                if (car != null) {
                    int productId = car.getProductId();
                    int quantity = car.getQuantity();
                    Product product = apProductMapper.selectById(productId);
                    int stock = Integer.parseInt(product.getStock());
                    product.setStock(String.valueOf(stock - quantity));
                    apProductMapper.updateById(product);
                }
                apCarMapper.deleteById(id);

            }

        }
        return ResponseResult.okResult("添加成功");
    }
}




